package net.realsee.service.wcs.data.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import net.realsee.common.utils.StringUtils;
import net.realsee.domain.wcs.data.WcsText;
import net.realsee.domain.wcs.data.bo.WcsTextBo;
import net.realsee.domain.wcs.data.vo.WcsTextVo;
import net.realsee.framework.core.domain.PageQuery;
import net.realsee.framework.core.page.TableDataInfo;
import net.realsee.mapper.wcs.data.WcsTextMapper;
import net.realsee.service.wcs.data.IWcsTextService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 文本IDService业务层处理
 *
 * @author realsee
 * @date 2023-08-11
 */
@RequiredArgsConstructor
@Service
public class WcsTextServiceImpl extends ServiceImpl<WcsTextMapper, WcsText> implements IWcsTextService {

    private final WcsTextMapper baseMapper;

    /**
     * 查询文本ID
     */
    @Override
    public WcsTextVo selectVoById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询文本ID列表
     */
    @Override
    public TableDataInfo<WcsTextVo> selectVoPage(WcsTextBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<WcsText> lqw = buildVoQueryWrapper(bo);
        Page<WcsTextVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询文本ID列表
     */
    @Override
    public List<WcsTextVo> selectVoList(WcsTextBo bo) {
        LambdaQueryWrapper<WcsText> lqw = buildVoQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<WcsText> buildVoQueryWrapper(WcsTextBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<WcsText> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getId() != null, WcsText::getId, bo.getId());
        lqw.eq(bo.getTextId() != null, WcsText::getTextId, bo.getTextId());
        lqw.eq(StringUtils.isNotBlank(bo.getFuid()), WcsText::getFuid, bo.getFuid());
        lqw.eq(bo.getRefrenceId() != null, WcsText::getRefrenceId, bo.getRefrenceId());
        lqw.eq(bo.getUnitId() != null, WcsText::getUnitId, bo.getUnitId());
        lqw.eq(bo.getDelFlag() != null, WcsText::getDelFlag, bo.getDelFlag());
        lqw.eq(StringUtils.isNotBlank(bo.getCreateBy()), WcsText::getCreateBy, bo.getCreateBy());
        lqw.eq(bo.getCreateTime() != null, WcsText::getCreateTime, bo.getCreateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getUpdateBy()), WcsText::getUpdateBy, bo.getUpdateBy());
        lqw.eq(bo.getUpdateTime() != null, WcsText::getUpdateTime, bo.getUpdateTime());
        lqw.eq(StringUtils.isNotBlank(bo.getDeleteBy()), WcsText::getDeleteBy, bo.getDeleteBy());
        lqw.eq(bo.getDeleteTime() != null, WcsText::getDeleteTime, bo.getDeleteTime());
        lqw.eq(bo.getRowVersionStamp() != null, WcsText::getRowVersionStamp, bo.getRowVersionStamp());
        return lqw;
    }

    /**
     * 新增文本ID
     */
    @Override
    public Boolean insertByBo(WcsTextBo bo) {
        WcsText add = BeanUtil.toBean(bo, WcsText.class);
        return this.save(add);
    }

    /**
     * 修改文本ID
     */
    @Override
    public Boolean updateByBo(WcsTextBo bo) {
        WcsText update = BeanUtil.toBean(bo, WcsText.class);
        return this.updateById(update);
    }

    /**
     * 批量删除文本ID
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids) {
        return this.removeBatchByIds(ids);
    }
}
